#coding=utf-8
#!/usr/bin/python
from bs4 import BeautifulSoup
from urllib import parse
from xpinyin import Pinyin
import re
import json
import requests                                 #导入requests包
class htmlParser(object):
 
    def getOtherInfo(self,name,content):
        if content.getText().find(name) > 0:
            return 1
        else:
            return 0

    def printInfo(self, name, content):
        info=""
        if content.getText().find(name) > 0:
            all_div = content.find_next_siblings()
            for adiv in all_div:
                paras = adiv.attrs
                for k, v in paras.items():
                    flag = 0
                    if type(v) is list:
                        if "anchor-list" in v:
                            flag = 1
                            # print(adiv)
                    if (flag == 1):
                        break

                else:
                    info=info+"%s" % (adiv)
                    continue
                break
            # print("%s" % ("\n==============学校" + name + "结束================\n"))
        return "%s" % (info)
    def printInfo3(self, name, content):
        info=""
        if content.getText().find(name) > 0:
            all_div = content.find_next_siblings()

            for adiv in all_div:
                paras = adiv.attrs
                for k, v in paras.items():
                    flag = 0
                    # all_div = all_div+all_table
                    if type(v) is list:
                        if "anchor-list" in v:
                            flag = 1
                            # print(adiv)
                    if (flag == 1):
                        break
                else:
                    info=info+'%s' % (adiv)
                    continue
                break
            # print("%s" % ("\n==============学校" + name + "结束================\n"))
        return '\n<h3 class="title-text">'+name+'</h3>\n%s' % (info)

    def parse(self,page_url,html_cont):
        if page_url is None or html_cont is None:
            return
 
        soup = BeautifulSoup(html_cont, "html.parser")
        dataHistory = ""#历史
        dataMajor= ""#专业、学科、院系、课程、学院
        dataAlumni=""#校友
        dataBuild = "" #教学建设 校园 校区
        dataSubject=""#优势学科：   优势 特色
        dataReputation="" #声誉
        dataEnvironmental="" #学校环境
        dataIncome="" #入学条件
        dataCulture="" #文化传统
        dataScholarship="" #奖助学金
        schoolName = ""
        introduce = ""
        summaryPic = ""
        baseInfoStr ={}
        dataCulture = ""  # 文化
        dataLearning = ""  # 学术研究  学术 科研
        dataResult={}
        titles=[]
        values=[]
        userTitles=["中文名",    "英文名","外文名", "中文名称",    "英文名称",  "建校年份", "简    称",              "创办时间",  "类    别", "现任校长" , "主要院系", "校    训",
                    "知名校友", "主要奖项",'中文简称','英文简称','别名','昵称']

        xpinyin = Pinyin()
        try:
            fh = open('百度百科（一阶段）五个学校示例数据.txt', 'a+')
            # 获取标题
            schoolName = soup.title.getText().replace("_百度百科","")
            try:
                summaryPic = soup.find('div', {'class': 'summary-pic'}).find('img').get("src")
            except Exception as e:
                print("error img")
            summary = soup.findAll('div', {'class': 'lemma-summary'})
            for k in summary:
                introduce = introduce+"%s" % (k)
            for k in soup.findAll('dt', {'class': 'basicInfo-item name'}):  # ,string='更多'
                titles.append(k.getText())
            for k in soup.findAll('dd', {'class': 'basicInfo-item value'}):  # ,string='更多'
                values.append(k.getText())
            me = dict(zip(titles, values))
            briefs = {}
            for (d, x) in me.items():
                if d in userTitles:
                    if d=="简    称":
                        brief = str(x).strip("\n").split("/")
                        if brief.__len__()==3:
                            briefs["中文简称"]=brief[0]
                            briefs["英文简称"]=brief[1]
                            briefs["别名"]=brief[2]
                        elif brief.__len__()==2:
                            briefs["中文简称"] = brief[0]
                            briefs["英文简称"]=brief[1]
                        elif brief.__len__()==1:
                            briefs["中文简称"] = brief[0]
            baseInfoResults = dict(me,**briefs)
            for (d, x) in baseInfoResults.items():
                if d in userTitles:
                    baseInfoStr[xpinyin.get_pinyin(d, splitter='').replace('    ','') ]=str(x).strip("\n")
            historys = soup.findAll('div', {'class': 'para-title level-2'})
            historys3 = soup.findAll('div', {'class': 'para-title level-3'})
            alumniList = ["杰出校友", "学术界", "商业", "娱乐", "活动家", "律师", "政府内阁成员", "法官", "立法会议员", "新闻界", "宗教", "体育"]
            for history in historys3:
                for alumni in alumniList:
                    result = self.getOtherInfo(alumni, history)
                    if result==1:
                        dataAlumni = dataAlumni + self.printInfo3(alumni, history)
                        break
                else:
                    continue

            dataBuild = self.level2(dataBuild, historys3, "设施")
            dataBuild = self.level2(dataBuild, historys3, "校园")
            dataBuild = self.level2(dataBuild, historys3, "校区")
            dataBuild = self.level2(dataHistory, historys3, "历史")
            dataMajor = self.level2(dataMajor, historys3, "专业")
            dataMajor = self.level2(dataMajor, historys3, "院系")
            dataMajor = self.level2(dataMajor, historys3, "办学")
            dataMajor = self.level2(dataMajor, historys3, "学科")
            dataMajor = self.level2(dataMajor, historys3, "课程")
            dataMajor = self.level2(dataMajor, historys3, "学院")
            dataAlumni = self.level2(dataAlumni, historys3, "校友")
            dataAlumni = self.level2(dataAlumni, historys3, "人才")
            dataAlumni = self.level2(dataAlumni, historys3, "杰出")
            dataBuild = self.level2(dataBuild, historys3, "建筑")
            dataSubject = self.level2(dataSubject, historys3, "优势")
            dataSubject = self.level2(dataSubject, historys3, "特色")
            dataReputation = self.level2(dataReputation, historys3, "誉")
            dataEnvironmental = self.level2(dataEnvironmental, historys3, "环境")
            dataIncome = self.level2(dataIncome, historys3, "入学")
            dataIncome = self.level2(dataIncome, historys3, "申请")
            dataCulture = self.level2(dataCulture, historys3, "体育")
            dataCulture = self.level2(dataCulture, historys3, "文化")
            dataCulture = self.level2(dataCulture, historys3, "社团")
            dataCulture = self.level2(dataCulture, historys3, "活动")
            dataScholarship = self.level2(dataScholarship, historys3, "奖学金")
            dataScholarship = self.level2(dataScholarship, historys3, "助学金")
            dataCulture = self.level2(dataCulture, historys3, "文化")
            dataLearning = self.level2(dataLearning, historys3, "学术")
            dataLearning = self.level2(dataLearning, historys3, "科研")
            # leve2
            dataLearning = self.level2(dataCulture, historys, "文化")
            dataLearning = self.level2(dataLearning, historys, "学术")
            dataLearning = self.level2(dataLearning, historys, "科研")
            dataBuild = self.level2(dataBuild, historys, "设施")
            dataBuild = self.level2(dataBuild, historys, "校园")
            dataBuild = self.level2(dataBuild, historys, "校区")
            dataHistory = self.level2(dataHistory, historys, "历史")
            dataMajor = self.level2(dataMajor, historys, "专业")
            dataMajor = self.level2(dataMajor, historys, "院系")
            dataMajor = self.level2(dataMajor, historys, "办学")
            dataMajor = self.level2(dataMajor, historys, "学科")
            dataMajor = self.level2(dataMajor, historys, "课程")
            dataMajor = self.level2(dataMajor, historys, "学院")
            dataAlumni = self.level2(dataAlumni, historys, "校友")
            dataAlumni = self.level2(dataAlumni, historys, "人才")
            dataAlumni = self.level2(dataAlumni, historys, "杰出")
            dataBuild = self.level2(dataBuild, historys, "建筑")
            dataSubject = self.level2(dataSubject, historys, "优势")
            dataSubject = self.level2(dataSubject, historys, "特色")
            dataReputation = self.level2(dataReputation, historys, "誉")
            dataEnvironmental = self.level2(dataEnvironmental, historys, "环境")
            dataIncome = self.level2(dataIncome, historys, "入学")
            dataIncome = self.level2(dataIncome, historys, "申请")
            dataCulture = self.level2(dataCulture, historys, "体育")
            dataCulture = self.level2(dataCulture, historys, "文化")
            dataCulture = self.level2(dataCulture, historys, "社团")
            dataCulture = self.level2(dataCulture, historys, "活动")
            dataScholarship = self.level2(dataScholarship, historys, "奖学金")
            scholarshipkeys = "助学金"
            dataScholarship = self.level2(dataScholarship, historys, scholarshipkeys)
            introduce = str(introduce).strip("\n")
            dataResult["schoolName"] = schoolName;
            dataResult["summaryPic"] = summaryPic;
            dataResult["introduce"] = introduce.replace('href="(.+)"(.*?)', "");
            dataResult["baseInfoStr"] = baseInfoStr;
            dataResult["dataHistory"] = dataHistory;  # 历史
            dataResult["dataMajor"] = dataMajor;  # 专业、学科、院系、课程、学院
            dataResult["dataAlumni"] = dataAlumni;  # 校友
            dataResult["dataBuild"] = dataBuild;  # 教学建设 校园 校区
            dataResult["dataSubject"] = dataSubject;  # 优势学科：   优势 特色
            dataResult["dataReputation"] = dataReputation;  # 声誉
            dataResult["dataEnvironmental"] = dataEnvironmental;  # 学校环境
            dataResult["dataIncome"] = dataIncome;  # 入学条件
            dataResult["dataCulture"] = dataCulture;  # 文化传统
            dataResult["dataScholarship"] = dataScholarship;  # 奖助学金
            dataResult["dataLearning"] = dataLearning;  # 学术研究  学术 科研

        except Exception as e:
            print(e)
            fh.close()

        return json.dumps(dataResult)

    def level2(self, dataScholarship, historys, scholarshipkeys):
        for history in historys:
            result = self.getOtherInfo(scholarshipkeys, history)
            if (result == 1):
                dataScholarship = dataScholarship + self.printInfo(scholarshipkeys, history)
                break
            else:
                continue
            break
        return dataScholarship
